/**
 * GenericComponentsDao.java
 *
 * Copyright (c) 2010 Prj.Manager All Rights Reserved.
 *
 * This source file may not be copied, modified or redistributed,
 * in whole or in part, in any form or for any reason, without the express
 * written consent of Prj.Manager .
 **/

package ro.prjmanager.dao;

import org.hibernate.Query;
import ro.prjmanager.core.dao.GenericDAO;
import ro.prjmanager.core.list.ListSqlParser;
import ro.prjmanager.core.list.SQL;
import ro.prjmanager.model.GenericComponents;
import ro.prjmanager.model.Projects;

import java.util.List;

/**
 * This interface provides custom method declaration for GenericComponents
 * entity and has a default implementation in the same package :
 * GenericComponentsDAOImpl
 *
 * @author Viorel Tacliciu
 * @version 1
 */

@SuppressWarnings({"unchecked"})
public class GenericComponentsDao extends GenericDAO<GenericComponents, java.lang.Integer> {

    @Override
    @SuppressWarnings("unchecked")
    public List<GenericComponents> listPagFilter(int startPage, int resPerPage, String sqlFilter) {
        StringBuilder sql = new StringBuilder("select c from ").append(getPersistentClass().getName()).append(" as c")

                .append(" join c.fkProjectLayer as d join d.fkProject as e");
        SQL sqlFilterParsed = ListSqlParser.parseSqlListParameter(sqlFilter);
        sql.append(" ").append(sqlFilterParsed.getBeforeWhereParent());
        sql.append(" where e.id=")
                .append(((Projects)getUserSession().getProject()).getId());
        sql.append(" ").append(sqlFilterParsed.getAfterWhereParent());

        Query sqlQuery = getSession().createQuery(sql.toString());
        sqlQuery.setMaxResults(resPerPage);
        sqlQuery.setFirstResult(startPage);

        return sqlQuery.list();
    }

    public List<GenericComponents> list() {
        StringBuilder sql = new StringBuilder("select c from ").append(getPersistentClass().getName()).append(" as c");
        sql.append(" join c.fkProjectLayer as d join d.fkProject as e");
        sql.append(" where e.id=")
                .append(((Projects)getUserSession().getProject()).getId());
        Query sqlQuery = getSession().createQuery(sql.toString());
        return sqlQuery.list();
    }


}
